package com.netrust.quarz.hw;

import java.util.HashMap;
import java.util.Map;

import com.netrust.quarz.hw.http.HttpsClient;
import com.netrust.quarz.hw.http.HttpsNode;
import com.netrust.quarz.hw.http.RequestEntity;
import com.netrust.quarz.hw.http.RequestType;
import com.netrust.quarz.hw.http.ResponseEntity;


public class HttpsClientUtil{

    /**
     * http请求
     * @param restURI rest接口URI
     * @param method 请求类型
     * @param content 请求消息内容
     * @param token  请求鉴权的TOKEN
     * @return 响应结果
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    //获取TOKEN请求
    public  Object sendRequest(String restURI, RequestType method, String content,NodeConfig nodeConf) throws Exception{
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("Accept", "application/json");
        headerMap.put("Content-Type",  "application/json;charset=UTF-8");
        return sendRequest(getNbiNode(nodeConf), headerMap, method, restURI, content,nodeConf);
    }
    
    //调业务接口请求
    public  Object sendRequest(String restURI, RequestType method, String content, String token,NodeConfig nodeConf) throws Exception{
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("Content-Type", "application/json;charset=utf-8");
        headerMap.put("Accept", "application/json;charset=UTF-8");
        if (null != nodeConf.getHostIP()) {
            headerMap.put("Host", nodeConf.getHostIP());
        }
        headerMap.put("X-Auth-Token", token);
        headerMap.put("User-Agent", "HttpClient");
        
        return sendRequest(getNbiNode(nodeConf), headerMap, method, restURI, content, nodeConf);
    }
    
    /**
     * HTTPS请求
     * 向指定的HTTP服务器发送https请求
     * @param node https服务节点
     * @param restURI REST URI
     * @param method http请求方法
     * @param content http请求内容
     * @param headerMap http请求Header信息  
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    public Object sendRequest(HttpsNode node, Map<String, String> headerMap, RequestType method, String restURI,String content,NodeConfig nodeConf) throws Exception{
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setNode(node);
        requestEntity.setReqType(method);
        
        String fullURL = String.format("https://%s:%s%s", node.getIp(), node.getPort(), restURI);
        requestEntity.setUrl(fullURL);
        requestEntity.setHeader(headerMap);
        if (content != null){
            requestEntity.setContent(content);
        }
        
        HttpsClient client=new HttpsClient();
		client.SetNodeConf(nodeConf);
        ResponseEntity responseEntity = client.sendRequest(requestEntity);
        Object responseMsg = responseEntity.getContent();
        
        return responseMsg;
    }
    
    private HttpsNode getNbiNode(NodeConfig nodeConf){
        HttpsNode NODE = new HttpsNode();

        NODE.setName("NBI");//自定义名称
        NODE.setIp(nodeConf.getOCIp());//OC访问IP
        NODE.setPort(nodeConf.getPort());//访问端口
        NODE.setNodeType("HTTPS");
        
        return NODE;
    }
}
